<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script> -->
    <script src="../node_modules/vue/dist/vue.global.js"></script>
</head>

<body>
    <div id="app">
        <!-- 实现一个组件通信案例 -->
        <!--使用子组件1 -->
        <child1 :pushfn="pushObjFn" :publisharr="publishArr"></child1>
        <!-- 使用子组件2 -->
        <child2 :publish-arr='publishArr'></child2>
    </div>
</body>

</html>
<script>
    // 将上述代码拆分成组件,实现组件化开发

    const { createApp } = Vue;
    const vm = createApp({
        data() {
            return {
                count: 0,
                publishArr: [ // 该数据就是父组件中axios 请求后的数据
                    {
                        id: 1,
                        username: '张三',
                        content: '我喜欢狂飙'
                    },
                    {
                        id: 2,
                        username: '李四',
                        content: '我喜欢三体'
                    }
                ]
            }
        },
        methods: {
            pushObjFn(m) {
                this.publishArr.push(m)
            }
        },
        components: {
            child1: {
                template: `<div>
                                <input type="text" name="" id="" v-model="username">
                                <input type="text" v-model="content">
                                <button @click="publishFn">发表</button>
                            </div>`,
                data() {
                    return {
                        username: '',
                        content: '',
                    }
                },
                props: ['pushfn', 'publisharr'],
                methods: {
                    // 方法1:
                    // publishFn() {
                    //     // console.log(this.$parent.publishArr);
                    //     this.$parent.publishArr.push({
                    //         id: this.$parent.publishArr[this.$parent.publishArr.length - 1].id + 1,
                    //         username: this.username,
                    //         content: this.content
                    //     })
                    // },
                    // 方法2: 接收 'pushfn', 'publisharr' 属性
                    publishFn() {
                        // 调用父组件中的方法. 
                        console.log(this.pushfn);
                        this.pushfn({
                            id: this.publisharr[this.publisharr.length - 1].id + 1,
                            username: this.username,
                            content: this.content
                        })
                    }
                }
            },
            child2: {
                template: `<ul>
                                <li v-for="item in publishArr" :key="item.id">
                                    <h2>发表人: {{item.username}}</h2>
                                    <p>发表内容: {{item.content}}</p>
                                </li>
                            </ul>`,
                props: ['publish-arr']

            }
        }



    });

    //挂载根实例到根节点
    vm.mount('#app')

</script>